package com.xxgc.ssm.controller;

import com.xxgc.ssm.po.info.Info;
import com.xxgc.ssm.po.req.UserLoginReg;
import com.xxgc.ssm.service.IUserLogin;
import com.xxgc.ssm.util.ErrorResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("/login")
@Api(tags = "用户注册登陆",description = "注册登录接口")
@ResponseBody
public class UserLoginController {
    //搭建一个通道
    @Autowired
    private IUserLogin uls;

    @PostMapping(value = "/register")
    @ApiOperation(value = "用户注册",notes = "你在调用的时候验证一下重复注册")
    public Info register(@RequestBody @Valid UserLoginReg.RegisterBean user,
                         BindingResult result,
                         @ApiIgnore Info info){
        info = ErrorResult.controller(result,info);
        return info != null ?  info : uls.insertRegister(user);
    }

    @PostMapping(value = "/loginByPhone")
    @ApiOperation(value = "用户登录",notes = "使用手机号和密码登录")
    public Info loginByPhone(@RequestBody @Valid UserLoginReg.LoginBean user,
                             BindingResult result,
                             @ApiIgnore HttpSession session,
                             @ApiIgnore Info info){
        System.out.println("效果");
        info = ErrorResult.controller(result,info);
        return info != null ?  info : uls.selectLoginByPhone(user,session);
    }
}
